from flask import jsonify, Blueprint
from flask_restful import reqparse, Resource, Api, marshal
from model.models import *
from utils.model_fields import *
from utils.cache import cache

user_bp = Blueprint('user', __name__, )
api = Api(user_bp)


class FriendResource(Resource):
    @cache.cached(5)
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('id')
        args = parser.parse_args()
        id = args.get('id')
        user = Friend.query.filter_by(user_id=id).all()
        print(user)
        list1 = []
        list2 = []
        for i in user:
            list2.append(i.friend_id)
        print("----------", list2)
        friend = User.query.filter(User.id.in_(list2)).all()
        print("fgj====================", friend)
        for i in friend:
            dict1 = {
                "name": i.name,
                "photo": i.photo,
            }
            list1.append(dict1)
        print("++++++++++++++++++++++++++++=", list1)
        list1.sort(key=lambda x: (x['name']))
        print(list2)
        return jsonify(message='获取成功', code=200, data=list1)


api.add_resource(FriendResource, '/friend')


class FriendRound(Resource):
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('id')
        args = parser.parse_args()
        id = args.get('id')
        friend_round = Circle4Friends.query.filter_by(user_id=id).order_by(Circle4Friends.add_time.desc()).all()
        print(friend_round)
        user = User.query.filter_by(id=id).first()
        friend_round = marshal(friend_round, friendround_fields)
        print(friend_round)
        data = {
            'name': user.name,
            'photo': user.photo,
            'signature': user.signature,
            'friend': friend_round
        }

        return jsonify(message='获取成功', code=200, data=data)


api.add_resource(FriendRound, '/friendround')


class ShieldFriend(Resource):
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('user_id')
        parser.add_argument('friend_id')
        args = parser.parse_args()
        user_id = args.get('user_id')
        friend_id = args.get('friend_id')

        friend = Friend.query.filter_by(user_id=user_id, friend_id=friend_id).first()

        if friend:
            if friend.status == 1:
                return jsonify(message='Ta的朋友圈对你不可见', code=200)
        return jsonify(message='对方还是不也是你的好友，请发起好友请求验证', code=204)


api.add_resource(ShieldFriend, '/shieldfriend')


class Shield(Resource):
    def put(self):
        parser = reqparse.RequestParser()
        parser.add_argument('user_id')
        parser.add_argument('friend_id')
        args = parser.parse_args()
        user_id = args.get('user_id')
        friend_id = args.get('friend_id')

        friend = Friend.query.filter_by(user_id=user_id, friend_id=friend_id).first()
        if friend:
            if friend.status == 1:
                Friend.query.filter_by(user_id=user_id, friend_id=friend_id).update({'status': 0})
                db.session.commit()
                return jsonify(message='取消屏蔽成功', code=200)
            else:
                Friend.query.filter_by(user_id=user_id, friend_id=friend_id).update({'status': 1})
                db.session.commit()
                return jsonify(message='已屏蔽', code=200)
        else:
            return jsonify(message='对方还是不也是你的好友，请发起好友请求验证', code=204)


api.add_resource(Shield, '/shield')
